//
//  FJProfileViewController.swift
//  FruitJuice
//
//  Created by Rae on 2023/12/15.
//

import UIKit

class FJProfileViewController: FJBaseViewController {

    private let backView            = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("FJ_Profile_bg").end()
    private let backButton          = c_make_ex(UIButton.self)
        .image("FJ_Profile_back").end()
    private let topView             = FJProfileTopView()
    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    
    private let dataSource: [FJProfileModel] = [.coins, .post, .friend, .black, .feedback, .setting]
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        topView.updateUserInfo()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigation.bar.isHidden = true
        
        view.subviews{
            backView.subviews{
                topView
                collectionView
            }
            backButton
        }
        
        backView.fillContainer()
        backButton.top(.statusHeight + 7).leading(15).size(30)
        topView.leading(0).trailing(0).height(135)
        topView.Top == backButton.Bottom
        collectionView.leading(0).trailing(0).bottom(0)
        collectionView.Top == topView.Bottom + 5
        
        backView.isUserInteractionEnabled = true
        let itemW = (.screenWidth-75.0)/2
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 15
        layout.minimumInteritemSpacing = 15
        layout.itemSize = CGSize(width: itemW, height: itemW)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 30.0, bottom: 0, right: 30.0)
        layout.scrollDirection = .vertical
        collectionView.style { v in
            v.collectionViewLayout = layout
            v.backgroundColor = .clear
            v.register(FJProfileItem.self, forCellWithReuseIdentifier: FJProfileItem.description())
            v.showsHorizontalScrollIndicator = false
            v.delegate = self
            v.dataSource = self
        }
        topView.editClosure = {
            self.navigationController?.pushViewController(FJEditViewController(), animated: true)
        }
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }
    
    @objc private func clickBackButton(){
        self.navigationController?.popViewController(animated: true)
    }

}

extension FJProfileViewController: UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let item = collectionView.dequeueReusableCell(withReuseIdentifier: FJProfileItem.description(), for: indexPath) as? FJProfileItem else { return UICollectionViewCell() }
        item.itemModel = dataSource[indexPath.row]
        return item
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        switch dataSource[indexPath.row]{
            case .coins:
                self.navigationController?.pushViewController(FJCoinsViewController(), animated: true)
                break;
            case .post:
                
                break;
            case .friend:
                self.navigationController?.pushViewController(FJFriendsListViewController(), animated: true)
                break;
            case .black:
                self.navigationController?.pushViewController(FJBlackListViewController(), animated: true)
                break;
            case .feedback:
                self.navigationController?.pushViewController(FJFeedbackViewController(), animated: true)
                break;
            case .setting:
                self.navigationController?.pushViewController(FJSettingViewController(), animated: true)
                break;
            default:
            return;
        }
    }
}

class FJProfileTopView: UIView {
    
    private let iconImageV            = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(22).bgColor(.lightGray).end()
    private let nameLabel            = c_make_ex(UILabel.self)
        .color(.black).font(25.font_heavy).text("").end()
    private let idLabel            = c_make_ex(UILabel.self)
        .color(.black).font(15.font_medium).text("").end()
    private let contentLabel            = c_make_ex(UILabel.self)
        .color("#B5B5B5").font(14.font_regular).line(2).text("Peel and cut oranges into juicer, addadda add  appropriate amoun…").end()
    private let editButton            = c_make_ex(UIButton.self)
        .image("FJ_Profile_edit").end()
    
    var editClosure: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = .clear
        
        subviews{
            iconImageV
            nameLabel
            idLabel
            contentLabel
            editButton
        }
        
        iconImageV.leading(20).top(12).size(90)
        nameLabel.leading(120).top(16.5).trailing(75)
        idLabel.leading(120).trailing(75)
        idLabel.Top == nameLabel.Bottom
        contentLabel.leading(120).trailing(25)
        contentLabel.Top == idLabel.Bottom
        editButton.trailing(20).size(34).top(17)
        
        editButton.addTarget(self, action: #selector(clickEditButton), for: .touchUpInside)
    }
    
    func updateUserInfo(){
        
        if let icon = USEINFOMODEL.headImage {
            self.iconImageV.loadImage(url: icon)
        }else{
            self.iconImageV.image = "FJ_Common_avator".toImage
        }
        if let name = USEINFOMODEL.name {
            self.nameLabel.text = name
        }else{
            self.nameLabel.text = "FruitJuice"
        }
        if let id = USEINFOMODEL.id {
            self.idLabel.text = "ID:" + id
        }else{
            self.idLabel.text = "ID:83185266"
        }
    }
    
    @objc private func clickEditButton(){
        editClosure?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class FJProfileItem: UICollectionViewCell {
    
    private let iconImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#B5B5B5").font(18.font_semiBold).alignment(.center).end()
    
    var itemModel: FJProfileModel = .none {
        didSet{
            iconImageView.image = itemModel.icon.toImage
            titleLabel.text = itemModel.title
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = "#EFEFEF".toColor
        self.layer.cornerRadius = 28.5
        self.layer.masksToBounds = true
        
        contentView.subviews {
            iconImageView
            titleLabel
        }

        iconImageView.top(26.5).size(55).centerHorizontally()
        titleLabel.centerHorizontally()
        titleLabel.Top == iconImageView.Bottom + 10
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
